package com.jianjian.service.impl;

import com.jianjian.Utils.SqlSessionFactoryUtil;
import com.jianjian.mapper.BrandMapper;
import com.jianjian.pojo.Brand;
import com.jianjian.pojo.PageBean;
import com.jianjian.service.BrandService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

/**
 * @author Zsir
 * @create 2021-12-06 21:29
 */
public class BrandServiceImpl implements BrandService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();

    //分页查询
    @Override
    public PageBean<Brand> selectAllPage(int currentPage, int pageSize) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        int begin = (currentPage -1) * pageSize;
        int size = pageSize;
        List<Brand> brands = mapper.selectAllPage(begin, size);
        int count1 = mapper.selectCount();

        PageBean<Brand> PageBean = new PageBean<>(brands,count1);
       sqlSession.close();
        return PageBean;
    }

    //分页条件查询
    @Override
    public PageBean<Brand> selectTPage(int currentPage, int pageSize, Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        int begin = (currentPage - 1) * pageSize;
        int size = pageSize;

        //处理brand条件，把对应的模糊表达式
        String brandName = brand.getBrandName();
        if (brandName !=null && brandName.length() >0){
            brand.setBrandName("%"+brandName+"%");
        }

        String companyName = brand.getCompanyName();
        if (companyName !=null && companyName.length() >0){
            brand.setCompanyName("%"+companyName+"%");
        }

        List<Brand> brands = mapper.selectTPage(begin, size, brand);
        int count = mapper.selectTCount();
        PageBean<Brand> pageBean = new PageBean<>(brands,count);
        sqlSession.close();
        return pageBean;
    }

    //    查询所有数据
    @Override
    public List<Brand> selectAllBrand() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = mapper.selectAllBrand();
        sqlSession.close();
        return brands;

    }
//批量删除
    @Override
    public void deleteByIdAllBrand(int [] id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.deleteByIdAllBrand(id);
        sqlSession.commit();
        sqlSession.close();

    }

    //添加一条数据
    @Override
    public void addBrand(Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.addBrand(brand);
        sqlSession.commit();
        sqlSession.close();
    }

    //修改一条数据
    @Override
    public void updateBrand(Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.updateBrand(brand);
        sqlSession.commit();
        sqlSession.close();
    }

   //删除一条数据
    @Override
    public void deleteById(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.deleteByIdBrand(id);
        sqlSession.commit();
        sqlSession.close();
    }


}
